#!/bin/bash
# PBM_DJVU_Center_1.sh ( 24 ) : This script , by Frank Trampe , crops a specified set of pbm images to a specified set of dimensions and converts them to djvu . The output file names are pre-determined , and the redimensioning process centers the cropping window . The intention is that this script ought to be part of a larger workflow that removes the temporary files after binding them . If enclosed in double quotations , the first argument can contain multiple file names and can contain wildcards .
# If we have a formatted pdf , we generate page images through xpdf's pdftoppm . It is very important that we either select the pages in this setup or that he remove the excess files before running this script .
# pdftoppm -f 1 -l 128 -mono -r 600 ./Input_1.pdf ./Image
# If we have a raster pdf that has one full monochrome image per page , we extract the images through xpdf's pdfimages .
# pdfimages -f 1 -l 128 ./Input_1.pdf ./Image
# This script handles the resulting files .
# PBM_DJVU_Center_1.sh "Image-*.pbm" 5100 6600 600
# We then need to bind the files .
# djvm -c ./Output_1.C.djvu ./Page_*.Cr.djvu ; djvm -c ./Output_1.Cl.djvu ./Page_*.Clr.djvu ; djvm -c ./Output_1.L.djvu ./Page_*.Lr.djvu ;
# Cr represents the converted files that can be removed . Clr represents the cleaned converted files that can be removed . Lr represents the lossy converted files that can be removed . For a multi-task workflow , it seems most helpful to change to a temporary directory for all of the internal steps in a conversion , extracting and processing page images there and writing the final , joined files back to the original directory .
# The first argument is the input file specification . The second argument is the output width . The third argument is the output width . The fourth argument is the output resolution , which affects not pixel count but physical dimensioning .
# This script requires that the PBM_Processor_1 and PBM_Info_1 utilities be in the system's path . It requires the cjb2 compressor , which is included in the djvulibre-bin package .
File1=$1 ;
Width2=$2 ;
Height2=$3 ;
Page1=0;
for FileA in $1
do
  let "Page1++" ;
  Loc1="`Number_Crop_1 4 $Page1`" ;
  File5="Page_$Loc1.Cr.djvu" ;
  File6="Page_$Loc1.Clr.djvu" ;
  File7="Page_$Loc1.Lr.djvu" ;
  Width1=`PBM_Info_1 --Width < $FileA` ;
  Height1=`PBM_Info_1 --Height < $FileA` ;
  if [[ $Width1 = $Width2 ]]
  then
    let "XOffset1 = ( ( Width1 - Width2 ) / 2 )" ;
    let "XOffset2 = 0" ;
    XTotal1=$Width2 ;
    XTotal2=$Width2 ;
  fi
  if [[ $Width1 > $Width2 ]]
  then
    let "XOffset1 = ( ( Width1 - Width2 ) / 2 )" ;
    let "XOffset2 = 0" ;
    XTotal1=$Width2 ;
    XTotal2=$Width2 ;
  fi
  if [[ $Width1 < $Width2 ]]
  then
    let "XOffset1 = 0" ;
    let "XOffset2 = ( ( Width2 - Width1 ) / 2 )" ;
    XTotal1=$Width1 ;
    XTotal2=$Width1 ;
  fi
  if [[ $Height1 = $Height2 ]]
  then
    let "YOffset1 = ( ( Height1 - Height2 ) / 2 )" ;
    let "YOffset2 = 0" ;
    YTotal1=$Height2 ;
    YTotal2=$Height2 ;
  fi
  if [[ $Height1 > $Height2 ]]
  then
    let "YOffset1 = ( ( Height1 - Height2 ) / 2 )" ;
    let "YOffset2 = 0" ;
    YTotal1=$Height2 ;
    YTotal2=$Height2 ;
  fi
  if [[ $Height1 < $Height2 ]]
  then
    let "YOffset1 = 0" ;
    let "YOffset2 = ( ( Height2 - Height1 ) / 2 )" ;
    YTotal1=$Height1 ;
    YTotal2=$Height1 ;
  fi
  echo $Page1
  echo "PBM_Processor_1 --ManualDim2 1 --Width2 $Width2 --Height2 $Height2 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $XTotal1 --YTotal1 $YTotal1 --XTotal2 $Width2 --YTotal2 $Height2 < $FileA > Temp_2.pbm" ;
  PBM_Processor_1 --ManualDim2 1 --Width2 $Width2 --Height2 $Height2 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $XTotal1 --YTotal1 $YTotal1 --XTotal2 $Width2 --YTotal2 $Height2 < $FileA > Temp_2.pbm ;
  cjb2 -dpi $4 Temp_2.pbm $File5 ;
  cjb2 -dpi $4 -clean Temp_2.pbm $File6 ;
  cjb2 -dpi $4 -lossy Temp_2.pbm $File7 ;
  rm ./Temp_2.pbm ;
done

